<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :model="loginForm"
      :rules="rules"
      ref="loginform"
    >
      <div class="title-container">
        <h3 class="title">{{ $t('msg.login.title') }}</h3>
        <!-- <lang-select class="lang-select" effect="light"></lang-select> -->
      </div>
      <!-- username -->
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon="user"></svg-icon>
        </span>
        <el-input
          v-model="loginForm.username"
          type="index"
          placeholder="username"
          name="username"
          clearable
        ></el-input>
      </el-form-item>
      <!-- password -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon="password"></svg-icon>
        </span>
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="Please input password"
          show-password
          name="password"
          clearable
        ></el-input>
      </el-form-item>
      <!-- 登录按钮 -->
      <el-button
        type="primary"
        style="width: 100%; margin-bottom: 30px;background:linear-gradient(to right, #FFE4E1,#DF7DA6,#BB9BCC);border-color: #FFE4E1;"
        @click="handlerlogin"
        :loading="loading"
        >{{ $t('msg.login.loginBtn') }}</el-button
      >
      <!-- <div class="tips" v-html="$t('msg.login.desc')"></div> -->
    </el-form>
  </div>
</template>

<script setup>
// 导入组件之后无需注册可直接使用
import { trigger } from '@vue/reactivity'
import { useStore } from 'vuex'
import { reactive, ref, computed } from 'vue'
import { rulesPassword } from './rules.js'
import { useI18n } from 'vue-i18n'
import { watchSwitchLang } from '@/utils/i18n'
import LangSelect from '@/components/LangSelect'
// 表单数据
const loginForm = reactive({
  username: '杨老板',
  password: '123456'
})
// 加载数据效果
const loading = ref(false)
// 表单ref
const loginform = ref(null)
// 声明store
const store = useStore()
// 声明i18n
const i18n = useI18n()
// 表单校验规则
const rules = reactive({
  username: [
    {
      required: true,
      message: i18n.t('msg.login.usernameRule'),
      message: computed(() => {
        return i18n.t('msg.login.usernameRule')
      }),
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: i18n.t('msg.login.passwordNecessaryRule'),
      message: computed(() => {
        return i18n.t('msg.login.passwordNecessaryRule')
      }),
      trigger: 'blur'
    },
    { validator: rulesPassword, trigger: 'blur' }
  ]
})
// 监听语言的变化
watchSwitchLang(() => {
  loginFromRef.value.validate()
})
// 登录的点击事件
const handlerlogin = () => {
  // 先判断校验是否通过
  loginform.value.validate((valid) => {
    if (!valid) {
      return
    }
    loading.value = true
    store
      .dispatch('user/login', loginForm)
      .then((res) => {
        loading.value = false
      })
      .catch((err) => {
        loading.value = false
      })
  })
}
</script>

<style lang="scss" scoped>
// $bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #606266;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background:linear-gradient(to right, #FFE4E1,#DF7DA6,#BB9BCC);
  overflow: hidden;

  .login-form {
    position: relative;
    width: 600px;
    max-width: 100%;
    padding: 50px 35px 0;
    margin: 220px auto 0;
    overflow: hidden;
    background:#fff;
    opacity: 0.5;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 20px;

    ::v-deep .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    ::v-deep .el-input {
      display: inline-block;
      height: 47px;
      width: 90%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }
    .tips {
      font-size: 16px;
      color: white;
      line-height: 20px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    :deep(.lang-select) {
      position: absolute;
      top: 4px;
      right: 0;
      background-color: #fff;
      font-size: 22px;
      padding: 4px;
      border-radius: 4px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
